<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/1/9
 * Time: 下午3:39
 */

namespace Biz\model\trade;

use Doraemon\tools\ImgUrl as toolImgUrl;
use Medoo\Medoo;

class OrderRefund extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_trade';

    const _DATABASE_ = 'shop_trade';

    const _TABLE_NAME = 'order_refund';

    //自动同意退款的时间  退款单创建时间+3天
    const AUTO_AGREE_TIME = 3 * 86400;

    //商家同意退货后，用户需在指定时间内填写物流信息
    const ALLOW_FILL_LOGISTICS_CLOSE_TIME = 7 * 86400;

    //商家同意退货后，用户需在指定时间内填写物流信息,否则系统自动取消退货
    const AUTO_CANCEL_TIME_FROM_AGREE_REFUND_GOODS = 7 * 86400;

    //商家同意退货后，买家发货后，自动收货时间   15天
    const AUTO_CONFIRM_RECEIPT_TIME = 15 * 86400;

    //商家同意退货后，买家发货后， 允许商家延长的收货时间   3天
    const ALLOW_PROLONG_RECEIPT_TIME = 3 * 86400;

    //卖家拒绝退款，7天后自动取消退款
    const AUTO_AFTER_REJECT_CANCEL_TIME = 7 * 86400;

    //买卖双方协商不一致，买家需在7天内发起维权，逾期系统将关闭入口
    const AUTO_AFTER_REJECT_RIGHTS_STOP_TIME = 7 * 86400;

    //买卖双方协商不一致，买家需在7天内修改退款申请
    const AUTO_AFTER_REJECT_CHANGE_FORM_TIME = 7 * 86400;


    //申请维权后，多长时间内不介入，则超时
    const RIGHTS_INTO_TIMEOUT_TIME = 1 * 86400;

    //商家介入后，72小时内可以举证，72小时以后开始判定
    const SUBMIT_PROOF_STOP_TIME = 72 * 3600;

    //状态停留在待判定超过72小时则视为超时
    const RIGHT_JUDGE_TIME_OUT = 72 * 3600;


    //退款类型
    const TYPE_REFUND_AMOUNT = 1;//退款
    const TYPE_REFUND_ALL = 2;//退货退款

    public static $typeRefundDesc = [
        self::TYPE_REFUND_AMOUNT => '仅退款',
        self::TYPE_REFUND_ALL => '退货退款'
    ];

    //退款状态
    const STATUS_NORMAL = 10;//待审核
    const STATUS_WAIT_REFUND_GOODS = 11;//待退货
    const STATUS_WAIT_CONFIRM_RECEIPT = 12;//待确认收货
    const STATUS_REFUNDING = 20;//退款中
    const STATUS_COMPLETE = 30;//退款完成
    const STATUS_CANCEL = 40;//退款取消
    const STATUS_RETURN_GOODS_CANCEL = 41;//退货取消
    const STATUS_REJECT = 50;//拒绝退款


    //退款进行中的状态
    public static $refundIngStatusList = [
        self::STATUS_NORMAL,
        self::STATUS_WAIT_REFUND_GOODS,
        self::STATUS_WAIT_CONFIRM_RECEIPT,
        self::STATUS_REFUNDING,
        self::STATUS_REJECT,
    ];


    //维权状态
    const RIGHTS_STATUS_NORMAL = 80;//待介入
    const RIGHTS_STATUS_PROOFING = 81;//举证中
    const RIGHTS_STATUS_JUDGE = 82;//维权判定
    const RIGHTS_STATUS_CANCEL = 83;//维权取消


    //维权进行中的状态列表
    public static $rightsIngStatusList = [
        self::RIGHTS_STATUS_NORMAL,
        self::RIGHTS_STATUS_PROOFING,
    ];


    //维权判定不退款的虚拟状态
    const V_RIGHTS_STATUS_NO_REFUND = 88;//维权判定不退款

    //维权处理72小时前后状态
    const RIGHTS_STATUS_PROOFING_BEFORE = 84; //未超过72小时（虚拟状态请勿插入数据库）
    const RIGHTS_STATUS_PROOFING_AFTER = 85;  //超过72小时（虚拟状态请勿插入数据库）


    /*--------状态描述----------*/
    public static $statusDesc = [
        self::STATUS_NORMAL => '待审核',
        self::STATUS_WAIT_REFUND_GOODS => '待退货',
        self::STATUS_WAIT_CONFIRM_RECEIPT => '待确认收货',
        self::STATUS_REFUNDING => '退款中',
        self::STATUS_COMPLETE => '退款完成',
        self::STATUS_CANCEL => '退款取消',
        self::STATUS_RETURN_GOODS_CANCEL => '退货取消',
        self::STATUS_REJECT => '拒绝退款',
        self::RIGHTS_STATUS_NORMAL => '待平台介入',
        self::RIGHTS_STATUS_PROOFING => '举证中',
        self::RIGHTS_STATUS_JUDGE => '维权结束',
        self::RIGHTS_STATUS_CANCEL => '维权取消',
        self::V_RIGHTS_STATUS_NO_REFUND => '维权结束',
    ];
    public static $statusMgtDesc = [
        self::STATUS_NORMAL => '待审核',
        self::STATUS_WAIT_REFUND_GOODS => '待买家退货',
        self::STATUS_WAIT_CONFIRM_RECEIPT => '待商家收货',
        self::STATUS_REFUNDING => '退款中',
        self::STATUS_COMPLETE => '退款完成',
        self::STATUS_CANCEL => '退款取消',
        self::STATUS_RETURN_GOODS_CANCEL => '退货取消',
        self::STATUS_REJECT => '拒绝退款',
        self::RIGHTS_STATUS_NORMAL => '待平台介入',
        self::RIGHTS_STATUS_PROOFING => '待处理',
        self::RIGHTS_STATUS_JUDGE => '维权结束',
        self::RIGHTS_STATUS_CANCEL => '维权取消',
        self::V_RIGHTS_STATUS_NO_REFUND => '维权结束',
    ];


    public static $statusAdmDesc = [
        self::STATUS_NORMAL => '待商家审核',
        self::STATUS_WAIT_REFUND_GOODS => '待买家退货',
        self::STATUS_WAIT_CONFIRM_RECEIPT => '待商家收货',
        self::STATUS_REFUNDING => '退款中',
        self::STATUS_COMPLETE => '退款完成',
        self::STATUS_CANCEL => '退款取消',
        self::STATUS_RETURN_GOODS_CANCEL => '退货取消',
        self::STATUS_REJECT => '拒绝退款',
        self::RIGHTS_STATUS_PROOFING => '待判定',
        self::RIGHTS_STATUS_NORMAL => '待介入',
        self::RIGHTS_STATUS_JUDGE => '判定完毕',
        self::RIGHTS_STATUS_CANCEL => '维权取消',
        self::V_RIGHTS_STATUS_NO_REFUND => '维权结束',
    ];

    //-----------维权状态的各平台描述----------
    public static $rightsStatusDesc = [
        self::RIGHTS_STATUS_NORMAL => '待平台介入',
        self::RIGHTS_STATUS_PROOFING => '举证中',
        self::RIGHTS_STATUS_JUDGE => '维权结束',
        self::RIGHTS_STATUS_CANCEL => '维权取消',
        self::V_RIGHTS_STATUS_NO_REFUND => '维权结束',

    ];
    public static $rightsStatusMgtDesc = [
        self::RIGHTS_STATUS_NORMAL => '待平台介入',
        self::RIGHTS_STATUS_PROOFING_BEFORE => '待处理72小时内',
        self::RIGHTS_STATUS_PROOFING_AFTER => '待处理超过72小时',
        self::RIGHTS_STATUS_PROOFING => '待处理',
        self::RIGHTS_STATUS_JUDGE => '维权结束',
        self::RIGHTS_STATUS_CANCEL => '维权取消',
        self::V_RIGHTS_STATUS_NO_REFUND => '维权结束',

    ];
    public static $rightsStatusAdmDesc = [
        self::RIGHTS_STATUS_NORMAL => '待介入',
        self::RIGHTS_STATUS_PROOFING => '待判定',
        self::RIGHTS_STATUS_JUDGE => '判定完毕',
        self::RIGHTS_STATUS_CANCEL => '维权取消',
        self::V_RIGHTS_STATUS_NO_REFUND => '维权结束',
    ];

    /*--------------------------------*/


    //-----------------APP 退款文案----------------
    public static $statusAmountViewDesc = [
        self::STATUS_NORMAL => '退款中',
        self::STATUS_REFUNDING => '退款中',
        self::STATUS_COMPLETE => '退款完成',
        self::STATUS_CANCEL => '退款',
        self::STATUS_RETURN_GOODS_CANCEL => '退款',
        self::STATUS_REJECT => '拒绝退款',
        self::RIGHTS_STATUS_NORMAL => '维权中',
        self::RIGHTS_STATUS_PROOFING => '维权中',
        self::RIGHTS_STATUS_JUDGE => '维权结束',
        self::RIGHTS_STATUS_CANCEL => '拒绝退款',
        self::V_RIGHTS_STATUS_NO_REFUND => '维权结束',

    ];

    public static $statusAllViewDesc = [
        self::STATUS_NORMAL => '退货中',
        self::STATUS_WAIT_REFUND_GOODS => '待退货',
        self::STATUS_WAIT_CONFIRM_RECEIPT => '退货中',
        self::STATUS_REFUNDING => '退款中',
        self::STATUS_COMPLETE => '退款完成',
        self::STATUS_CANCEL => '退款',
        self::STATUS_RETURN_GOODS_CANCEL => '退款',
        self::STATUS_REJECT => '拒绝退款',
        self::RIGHTS_STATUS_NORMAL => '维权中',
        self::RIGHTS_STATUS_PROOFING => '维权中',
        self::RIGHTS_STATUS_JUDGE => '维权结束',
        self::RIGHTS_STATUS_CANCEL => '拒绝退款',
        self::V_RIGHTS_STATUS_NO_REFUND => '维权结束',

    ];
    /*--------------------------------*/

    //----------------商家后台列表详情展示文案-----------------

    public static $statusMgtListAndDetailDesc = [
        self::STATUS_NORMAL => '待审核',
        self::STATUS_REJECT => '拒绝退款',
        self::STATUS_WAIT_REFUND_GOODS => '待买家退货',
        self::STATUS_WAIT_CONFIRM_RECEIPT => '待商家收货',
        self::STATUS_COMPLETE => '退款完成',
        self::RIGHTS_STATUS_CANCEL => '拒绝退款',
        self::RIGHTS_STATUS_NORMAL => '维权中',
        self::RIGHTS_STATUS_PROOFING => '维权中',
        self::STATUS_REFUNDING => '退款中',
        self::V_RIGHTS_STATUS_NO_REFUND => '维权结束',
        self::RIGHTS_STATUS_JUDGE => '维权结束'
    ];

    /*----------------------------------------------------*/


    //退款途径
    const REFUND_WAY_ALIPAY_APP = 'alipay';//支付宝APP支付
    const REFUND_WAY_WXPAY_APP = 'wechat';//微信APP支付

    public static $payWayDesc = [
        self::REFUND_WAY_ALIPAY_APP => '支付宝支付',
        self::REFUND_WAY_WXPAY_APP => '微信支付',
    ];

    //收货状态
    const RECEIPT_STATUS_NORMAL = 1;//未发货
    const RECEIPT_STATUS_NO = 2;//未收到货
    const RECEIPT_STATUS_YES = 3;//已收到货

    public static $receiptStatusDesc = [
        self::RECEIPT_STATUS_NORMAL => '未发货',
        self::RECEIPT_STATUS_NO => '未收到货',
        self::RECEIPT_STATUS_YES => '已收到货',
    ];


    //维权判定结果
    const RIGHTS_JUDGE_RESULT_USER = '1';//用户胜诉
    const RIGHTS_JUDGE_RESULT_MGT = '2';//商家胜诉

    public static $rightsJudgeResultDesc = [
        self::RIGHTS_JUDGE_RESULT_USER => '买家胜诉',
        self::RIGHTS_JUDGE_RESULT_MGT => '商家胜诉',
    ];

    //维权判定处理方式
    const RIGHTS_PROCESS_METHOD_NO_REFUND = '1';//不退款
    const RIGHTS_PROCESS_METHOD_REFUND_AMOUNT = '2';//退款
    const RIGHTS_PROCESS_METHOD_REFUND_ALL = '3';//退货退款

    public static $rightsProcessMethodDesc = [
        self::RIGHTS_PROCESS_METHOD_NO_REFUND => '不退款',
        self::RIGHTS_PROCESS_METHOD_REFUND_AMOUNT => '退款',
        self::RIGHTS_PROCESS_METHOD_REFUND_ALL => '退货退款'
    ];

    //退款原因 常亮
    const REFUND_REASON_35 = 35;
    const REFUND_REASON_30 = 30;
    const REFUND_REASON_40 = 40;


    public static $reasonList = [
        'group-1' => [
            10 => '拍错了/订单信息错误',
            11 => '不想要了',
            12 => '商家缺货',
            13 => '发货时间太长不等了',
            14 => '协商一致退款',
            15 => '其他'
        ],
        'group-2' => [
            20 => '长时间未到货',
            21 => '空包裹',
            22 => '包裹破损已拒签'
        ],
        'group-3' => [
            self::REFUND_REASON_30 => '退运费',
            31 => '商品与描述不符',
            32 => '收到的商品破损',
            33 => '商品错发/漏发',
            34 => '商品质量问题',
            self::REFUND_REASON_35 => '7天价保退差价'
        ],
        'group-4' => [
            40 => '7天无理由退货',
            31 => '商品与描述不符',
            32 => '收到的商品破损',
            33 => '商品错发/漏发',
            34 => '商品质量问题',
        ]
    ];


    public static function getRefundOrderList($refundOrderNoList = [], $from = '')
    {
        $list = empty($refundOrderNoList) ? [] : self::select('*', ['refund_order_no' => $refundOrderNoList]);

        $list = array_map(function ($info) use ($from) {
            self::formatInfo($info, $from);

            return $info;
        }, $list);

        return array_column($list, null, 'refund_order_no');
    }

    public static function getRefundOrderInfo($refundOrderNo, $from = '')
    {
        $info = self::get('*', ['refund_order_no' => $refundOrderNo]);
        if (empty($info)) {
            return [];
        }

        self::formatInfo($info, $from);

        return $info;
    }

    public static function getRefundOrderInfoFromMaster($refundOrderNo)
    {
        $info = self::select_master("*", Medoo::raw(' where refund_order_no=:refundOrderNo for update', [':refundOrderNo' => $refundOrderNo]));

        $info = array_shift($info);


        if (empty($info)) {
            return false;
        }

        self::formatInfo($info);

        return $info;
    }


    public static function formatInfo(&$info, $from = '')
    {
        if (empty($info)) {
            return;
        }

        $reasonList = self::getAllRefundReasonList();

        foreach ($info as $key => &$value) {
            switch ($key) {
                case 'cover_img':
                    $info['cover_img_url'] = toolImgUrl::getUrl(toolImgUrl::APPTYPE_BIZ, $value, toolImgUrl::IMAGE_SIZE_ORI_NPHONE);
                    $info['cover_img_thumb_url'] = toolImgUrl::getUrl(toolImgUrl::APPTYPE_BIZ, $value, toolImgUrl::IMAGE_SIZE_SQ_THUMB);
                    break;
                case 'type_refund':
                    $info['type_refund_desc'] = $value > 0 ? self::$typeRefundDesc[$value] : '';
                    break;
                case 'receipt_status':
                    $info['receipt_status_desc'] = $value > 0 ? self::$receiptStatusDesc[$value] : '';
                    break;
                case 'reason':
                    $info['reason_desc'] = $value > 0 ? $reasonList[$value] : '';
                    break;
                case 'rights_process_method':
                    $info['rights_process_method_desc'] = $value > 0 ? self::$rightsProcessMethodDesc[$value] : '';
                    break;
                case 'rights_judge_result':
                    $info['rights_judge_result_desc'] = $value > 0 ? self::$rightsJudgeResultDesc[$value] : '';
                    break;
                case 'refund_images_content':
                    if (!empty($value)) {
                        $value = json_decode($value, true);
                        if (!empty($value)) {
                            $value = array_map(function ($v) {
                                return [
                                    'id' => $v,
                                    'url' => toolImgUrl::getUrl(toolImgUrl::APPTYPE_BIZ, $v, toolImgUrl::IMAGE_SIZE_ORI_NPHONE),
                                    'thumb_url' => toolImgUrl::getUrl(toolImgUrl::APPTYPE_BIZ, $v, toolImgUrl::IMAGE_SIZE_SQ_THUMB)
                                ];
                            }, $value);
                        }
                    }
                    $info['refund_images_content_url'] = is_array($value) ? $value : [];
                    break;
                case 'reject_images_content':
                    if (!empty($value)) {
                        $value = json_decode($value, true);
                        $value = array_map(function ($v) {
                            return [
                                'id' => $v,
                                'url' => toolImgUrl::getUrl(toolImgUrl::APPTYPE_BIZ, $v, toolImgUrl::IMAGE_SIZE_ORI_NPHONE),
                                'thumb_url' => toolImgUrl::getUrl(toolImgUrl::APPTYPE_BIZ, $v, toolImgUrl::IMAGE_SIZE_SQ_THUMB)
                            ];
                        }, $value);

                    }
                    $info['reject_images_content_url'] = is_array($value) ? $value : [];
                    break;
                case 'rights_images_content':
                    if (!empty($value)) {
                        $value = json_decode($value, true);
                        $value = array_map(function ($v) {
                            return [
                                'id' => $v,
                                'url' => toolImgUrl::getUrl(toolImgUrl::APPTYPE_BIZ, $v, toolImgUrl::IMAGE_SIZE_ORI_NPHONE),
                                'thumb_url' => toolImgUrl::getUrl(toolImgUrl::APPTYPE_BIZ, $v, toolImgUrl::IMAGE_SIZE_SQ_THUMB)
                            ];
                        }, $value);

                    }
                    $info['rights_images_content_url'] = is_array($value) ? $value : [];
                    break;
                case 'rights_judge_images_content':
                    if (!empty($value)) {
                        $value = json_decode($value, true);
                        $value = array_map(function ($v) {
                            return [
                                'id' => $v,
                                'url' => toolImgUrl::getUrl(toolImgUrl::APPTYPE_BIZ, $v, toolImgUrl::IMAGE_SIZE_ORI_NPHONE),
                                'thumb_url' => toolImgUrl::getUrl(toolImgUrl::APPTYPE_BIZ, $v, toolImgUrl::IMAGE_SIZE_SQ_THUMB)
                            ];
                        }, $value);

                    }
                    $info['rights_judge_images_content_url'] = is_array($value) ? $value : [];
                    break;
            }
        }


        //格式化状态码
        self::formatStatusAndDesc($info, $from);

    }

    //注意：需要status,rights_status,rights_process_method,submit_proof_stop_time 这几个字段
    private static function formatStatusAndDesc(&$info, $from = '')
    {
        //虚拟状态的格式化
        if (!isset($info['status'])) {
            return;
        }


        //初始化退款各状态
        $info['v_status'] = $info['status'];

        //初始化维权状态
        $info['v_rights_status'] = $info['rights_status'];

        //初始化格式化参数
        $info['status_desc'] = '';
        $info['v_status_desc'] = '';

        $info['rights_status_desc'] = '';
        $info['v_rights_status_desc'] = '';


        if (isset($info['rights_status']) && $info['rights_status'] > 0) {

            $info['v_status'] = $info['rights_status'];

            //当维权结束后，虚拟状态要转换到退款状态中
            if (in_array($info['rights_status'], [self::RIGHTS_STATUS_JUDGE, self::RIGHTS_STATUS_CANCEL])) {
                $info['v_status'] = $info['status'];
            }

            //当维权判定结果为不退款时状态如下
            if ($info['rights_status'] == self::RIGHTS_STATUS_JUDGE && $info['rights_process_method'] == self::RIGHTS_PROCESS_METHOD_NO_REFUND) {
                $info['v_status'] = self::V_RIGHTS_STATUS_NO_REFUND;

                if ($from != 'mgt') {
                    $info['v_rights_status'] = self::V_RIGHTS_STATUS_NO_REFUND;
                }
            }


            //商家端需要判断是否在72小时内
            if ($from == 'mgt' && isset($info['submit_proof_stop_time'])) {
                if ($info['rights_status'] == self::RIGHTS_STATUS_PROOFING) {
                    $submitAutoStopTime = strtotime($info['submit_proof_stop_time']);
                    if ($submitAutoStopTime > time()) {
                        $info['v_rights_status'] = self::RIGHTS_STATUS_PROOFING_BEFORE;
                    } else {
                        $info['v_rights_status'] = self::RIGHTS_STATUS_PROOFING_AFTER;
                    }
                }
            }


            //渲染状态码对应的描述
            if ($from == 'adm') {
                $info['rights_status_desc'] = self::$rightsStatusAdmDesc[$info['rights_status']];
                $info['v_rights_status_desc'] = self::$rightsStatusAdmDesc[$info['v_rights_status']];

            } elseif ($from == 'mgt') {
                $info['rights_status_desc'] = self::$rightsStatusMgtDesc[$info['rights_status']];
                $info['v_rights_status_desc'] = self::$rightsStatusMgtDesc[$info['v_rights_status']];
            } else {
                $info['rights_status_desc'] = self::$rightsStatusDesc[$info['rights_status']];
                $info['v_rights_status_desc'] = self::$rightsStatusDesc[$info['v_rights_status']];

            }

        }


        //渲染状态码对应的描述
        if ($from == 'adm') {
            $info['status_desc'] = self::$statusAdmDesc[$info['status']];
            $info['v_status_desc'] = self::$statusAdmDesc[$info['v_status']];
        } elseif ($from == 'mgt') {
            $info['status_desc'] = self::$statusMgtDesc[$info['status']];
            $info['v_status_desc'] = self::$statusMgtDesc[$info['v_status']];

            if ($info['rights_status'] == self::RIGHTS_STATUS_CANCEL) {
                $info['rights_status'] = $info['v_rights_status'] = 0;
                $info['rights_status_desc'] = $info['v_rights_status_desc'] = '';
            }

            if ($info['status'] == self::STATUS_CANCEL) {
                $info['rights_status'] = $info['v_rights_status'] = 0;
                $info['rights_status_desc'] = $info['v_rights_status_desc'] = '';
            }
        } else {

            $info['v_h5_status'] = $info['v_app_status'] = $info['v_status'];

            $info['status_desc'] = self::$statusDesc[$info['status']];
            $info['v_status_desc'] = self::$statusDesc[$info['v_status']];

            $info['v_h5_status_desc'] = self::$statusDesc[$info['v_h5_status']];

            if ($info['type_refund'] == self::TYPE_REFUND_AMOUNT) {
                $info['v_app_status_desc'] = self::$statusAmountViewDesc[$info['v_app_status']];
            } elseif ($info['type_refund'] == self::TYPE_REFUND_ALL) {
                $info['v_app_status_desc'] = self::$statusAllViewDesc[$info['v_app_status']];
            } else {
                $info['v_app_status_desc'] = '';
            }
        }


    }

    private static function formatMgtDetailAndList(&$info)
    {
        foreach ($info as $key => $value) {
            switch ($key) {
                case 'status':
                    $info['status_desc'] = isset(self::$statusMgtListAndDetailDesc[$value]) ? self::$statusMgtListAndDetailDesc[$value] : '';
                    break;
                case 'rights_status':

                    $info['rights_status_desc'] = isset(self::$statusMgtListAndDetailDesc[$value]) ? self::$statusMgtListAndDetailDesc[$value] : '';
                    break;
            }
        }
    }

    public static function createOrEdit($orderRefundNo, $data, $scenes)
    {

        if ($scenes == 'create') {
            $result = self::insert($data);

            if ($result->rowCount() == 0) {
                throw new \Exception('退款申请错误');
            }


        } else {
            $result = self::update($data, ['refund_order_no' => $orderRefundNo]);
        }
    }

    public static function getAllRefundReasonList()
    {
        $result = [];

        $reasonList = self::$reasonList;

        array_walk($reasonList, function ($v) use (&$result) {
            foreach ($v as $key => $reason) {
                $result[$key] = $reason;
            }
        });

        return $result;
    }


    public static function getRefundCompleteListByOrderNoList($orderNo)
    {
        $list = self::select('*', ['order_no' => $orderNo, 'status' => self::STATUS_COMPLETE]);

        if (empty($list)) {
            return [];
        }
        $result = [];
        foreach ($list as $info) {
            self::formatInfo($info);
            $result[$info['order_no']][$info['sku_id']] = $info;
        }

        return $result;
    }

    public static function getRefundListByOrderNoForMgtDetailAndList($orderNo)
    {
        $list = self::select('*', ['order_no' => $orderNo]);

        if (empty($list)) {
            return [];
        }

        $result = [];

        foreach ($list as $info) {
            //self::formatMgtDetailAndList($info);
            self::formatStatusAndDesc($info, 'mgt');
            $result[$info['order_no']][$info['sku_id']] = $info;
        }
        return $result;
    }

    public static function getRefundInfoByOrderNo($orderNo)
    {
        $info = self::get('*', ['order_no' => $orderNo]);

        if (empty($info))
            return false;

        self::formatInfo($info);

        return $info;

    }

    public static function getRefundListByOrderNosByStatus($orderNos, $status)
    {
        $list = self::select('*', ['order_no' => $orderNos, 'status' => $status]);

        if (empty($list)) {
            return [];
        }
        return $list;
    }

    public static function getFirstRefundingSkuInfo($orderNo, $whereSkuIds = [])
    {
        $whereAnd = [
            'order_no' => $orderNo,
            'OR' => [
                'status' => self::STATUS_NORMAL,
                'AND #1' => [
                    'status' => self::STATUS_REJECT,
                    'rights_status[!]' => [self::RIGHTS_STATUS_CANCEL, self::RIGHTS_STATUS_JUDGE]
                ],
                'AND #2' => [
                    'status' => self::STATUS_REJECT,
                    'rights_status' => 0
                ]
            ]
        ];

        $whereAnd = array_merge($whereAnd, $whereSkuIds);

        $skuInfo = self::get('*', [
            'AND' => $whereAnd,
            'ORDER' => ['addtime' => 'ASC'],
        ]);

        if (!empty($skuInfo)) {
            self::formatInfo($skuInfo);
        }

        return $skuInfo;
    }

    public static function getClearRightsInfoDataStruct()
    {

        return [
            'launch_rights_time' => '0000-00-00 00:00:00',
            'rights_into_timeout_time' => '0000-00-00 00:00:00',
            'rights_into_time' => '0000-00-00 00:00:00',
            'rights_cancel_time' => '0000-00-00 00:00:00',
            'submit_proof_stop_time' => '0000-00-00 00:00:00',


            'rights_remark' => '',
            'rights_images_content' => '',


            'rights_judge_result' => 0,
            'rights_process_method' => 0,
            'rights_judge_remark' => '',
            'rights_judge_adminuid' => 0,
            'rights_judge_images_content' => '',
            'rights_judge_refund_amount' => 0,
            'rights_judge_refund_activity_allowance' => 0,
            'rights_judge_refund_integral' => 0,
            'rights_judge_refund_integral_amount' => 0,
            'rights_judge_refund_packet_amount' => 0,
            'rights_judge_refund_rmb' => '',
            'rights_judge_time' => '0000-00-00 00:00:00',


            'rights_status' => 0
        ];
    }

    public static function getRefundedOrRefundingList($orderNos, $from = '', $indexKey = 'refund_order_no')
    {
        $list = self::select('*', ['order_no' => $orderNos, 'status[!]' => [self::STATUS_CANCEL, self::STATUS_RETURN_GOODS_CANCEL]]);

        if (empty($list)) {
            return [];
        }

        $list = array_map(function ($v) use ($from) {
            if ($from == 'mgt') {
                self::formatMgtDetailAndList($v);
            } else {
                self::formatStatusAndDesc($v, $from);
            }

            if ($v['status'] == self::STATUS_REJECT) {
                $v['status_desc'] = $v['rights_status_desc'];
            }

            return $v;
        }, $list);

        return array_column($list, null, $indexKey);
    }

    public static function getRefundSuccessList($orderNo, $skuIds = [])
    {
        $where = ['order_no' => $orderNo, 'status' => [self::STATUS_COMPLETE, self::STATUS_REFUNDING]];

        if (!empty($skuIds)) {
            $where['sku_id'] = $skuIds;
        }

        $list = self::select('*', $where);

        if (empty($list)) {
            return [];
        }

        $result = array_column($list, null, 'sku_id');

        return $result;
    }

    //判断指定订单是否全部退款成功，且全额退款
    public static function checkIsFullRefund($orderNo, $payAmount)
    {
        //如果商品中的全部商品都已同意退款  查询订单的总成功的退款金额
        $refundSum = self::get(['refund_sum' => Medoo::raw('SUM(refund_rmb+refund_shipping_amount)')], [
            'order_no' => $orderNo,
            'status' => [
                self::STATUS_REFUNDING,
                self::STATUS_COMPLETE
            ]
        ]);

        //如果合计退款金额 == 支付金额
        if ($refundSum != null) {
            if ($refundSum['refund_sum'] == $payAmount) {
                return true;
            }
        }
        return false;

    }

    public static function getRefundInfoByWhere($method, $fields, $where)
    {
        return self::$method($fields, $where);

    }

    public static function updateData($data, $where)
    {
        return self::update($data, $where)->rowCount();
    }

}